﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2017/10/24 9:19:20------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;
using Newtonsoft.Json;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class coupons_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected string status = string.Empty;
        protected string keywords = string.Empty;
        protected string expiration_form = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.pageSize = GetPageSize(10); //每页数量
            this.keywords = DTRequest.GetQueryString("keywords");
            status = DTRequest.GetQueryString("status");
            expiration_form = DTRequest.GetQueryString("expiration_form");

            if (!Page.IsPostBack)
            {
                ChkAdminLevel("coupons", DTEnums.ActionEnum.View.ToString()); //检查权限
                RptBind("id>0" + CombSqlTxt(keywords), "id desc");
            }
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            txtKeywords.Text = this.keywords;

            ddlStatus.SelectedValue = status;
            ddlExpiration_form.SelectedValue = expiration_form;

            BLL.coupons bll = new BLL.coupons();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("coupons_list.aspx", "status={0}&expiration_form={1}&keywords={2}&page={3}",
                status, expiration_form, txtKeywords.Text, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");

            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords + "%' or contents like '%" + _keywords + "%')");
            }

            if (status != "")
            {
                switch (status)
                {
                    case "z":
                        strTemp.Append(" and status=0");
                        break;
                    case "j":
                        strTemp.Append(" and status=1");
                        break;
                }
            }

            if (expiration_form != "")
            {
                switch (expiration_form)
                {
                    case "zd":
                        strTemp.Append(" and expiration_form=0");
                        break;
                    case "st":
                        strTemp.Append(" and expiration_form=1");
                        break;
                    case "yb":
                        strTemp.Append(" and expiration_form=2");
                        break;
                }
            }


            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("coupons_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        private void Redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("coupons_list.aspx", "status={0}&expiration_form={1}&keywords={2}",
                    ddlStatus.SelectedValue, ddlExpiration_form.SelectedValue, txtKeywords.Text));
        }

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Redirect();
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("coupons_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            Redirect();
        }

        #region 下拉选择条件
        /// <summary>
        /// 状态过滤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }

        /// <summary>
        /// 过期形式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlExpiration_form_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }
        #endregion

        #region 启用、禁用
        /// <summary>
        /// 启用优惠券
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUsed_Click(object sender, EventArgs e)
        {
            Audit_Coupons(false);
        }

        /// <summary>
        /// 禁用优惠券
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLocked_Click(object sender, EventArgs e)
        {
            Audit_Coupons(true);
        }

        private void Audit_Coupons(bool isLock)
        {
            ChkAdminLevel("coupons", DTEnums.ActionEnum.Audit.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.coupons bll = new BLL.coupons();
            Model.coupons coupons = new Model.coupons();

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    coupons = bll.GetModel(id);

                    if (coupons != null && bll.UpdateField(id, (isLock ? "status=1" : "status=0")))
                    {
                        if (isLock)
                        {
                            AddAdminLog(DTEnums.ActionEnum.Audit.ToString(), "禁用优惠券 " + JsonConvert.SerializeObject(coupons)); //记录日志

                        }
                        else
                        {
                            AddAdminLog(DTEnums.ActionEnum.Audit.ToString(), "启用优惠券成功" + JsonConvert.SerializeObject(coupons)); //记录日志

                        }
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }

            if (isLock)
            {
                JscriptMsg("成功禁用优惠券" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("coupons_list.aspx", "status={0}&expiration_form={1}&keywords={2}", ddlStatus.SelectedValue, expiration_form, txtKeywords.Text));
            }
            else
            {
                JscriptMsg("成功启用优惠券" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("coupons_list.aspx", "status={0}&expiration_form={1}&keywords={2}", ddlStatus.SelectedValue, expiration_form, txtKeywords.Text));
            }
        }

        #endregion

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("coupons", DTEnums.ActionEnum.Audit.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.coupons bll = new BLL.coupons();
            Model.coupons coupons = new Model.coupons();

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    coupons = bll.GetModel(id);

                    if (coupons != null && bll.Delete(id))
                    {
                        AddAdminLog(DTEnums.ActionEnum.Audit.ToString(), "删除优惠券 " + JsonConvert.SerializeObject(coupons)); //记录日志
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }


            JscriptMsg("成功删除优惠券" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("coupons_list.aspx", "status={0}&expiration_form={1}&keywords={2}", ddlStatus.SelectedValue, expiration_form, txtKeywords.Text));

        }
    }
}

